﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NRemedy;
using NRemedy.Linq;
using ARNative;
using System.Configuration;
using NRemedy.MVC.UI;
using System.Reflection;
namespace RemedyMVC.Controllers
{
    public class JournalController : SessionController
    {
        private static readonly string ARserver = ConfigurationManager.AppSettings["ARserver"];
        private static readonly string ARuser = ConfigurationManager.AppSettings["ARuser"];
        private static readonly string ARpwd = ConfigurationManager.AppSettings["ARpwd"];


        public ActionResult Index()
        {
            //string Operate = Request["Operate"];
            //string RequestID = Request["JournalNumber"];

            List<WS_Incident_Journal> JournalList = null;
            //using (ARLoginContext context = new ARLoginContext(ARserver, ARuser, ARpwd))
            //{
            //    ARProxy<WS_Incident_Journal> proxy = new ARProxy<WS_Incident_Journal>(context);
            //    if (Operate == "Delete" && !string.IsNullOrEmpty(RequestID))
            //    {
            //        //删除对应的日志
            //        proxy.DeleteEntry(new WS_Incident_Journal { RequestID = RequestID });
            //    }

            //    //查询日志列表
            //    var q = from s in new ARSet<WS_Incident_Journal>(context)
            //            select s;
            //    JournalList = q.ToList();


            //}

            return View("Index", JournalList);


        }

        public ActionResult GetIncidentJournalList(string ParentOBJID)
        {

            List<WS_Incident_Journal> JournalList = null;
            using (ARLoginContext context = new ARLoginContext(ARserver, ARuser, ARpwd))
            {

                //查询日志列表
                var q = from s in new ARSet<WS_Incident_Journal>(context)
                        where s.ParentObejctID == ParentOBJID
                        select s;
                JournalList = q.ToList();

            }
            IList<object> JournalList_json = new List<object>();
            foreach (var m in JournalList)
            {
                JournalList_json.Add(new { requestid = m.RequestID, category = m.Category, subject = m.Subject, createdby = m.CreateBy, createtime = m.CreateTime });
            }
            return Json(JournalList_json);

        }

        [HttpGet]
        public ActionResult JournalDetail(WS_Incident_Journal WS_Incident_Journal)
        {
            string Operate = Request["Operate"];
            string RequestID = Request["RequestID"];
            ViewData["RequestID"] = RequestID;
            WS_Incident_Journal = new WS_Incident_Journal();
            if (Operate == "Add")
            {   //新增时RequestID表示事件的RequestID
                WS_Incident_Journal.ParentObejctID = RequestID;
                ViewData["Operate"] = "Add";
            }
            else
            {
                if (!string.IsNullOrEmpty(RequestID))
                {
                    using (ARLoginContext context = new ARLoginContext(ARserver, ARuser, ARpwd))
                    {
                        ARProxy<WS_Incident_Journal> proxy = new ARProxy<WS_Incident_Journal>(context);
                        WS_Incident_Journal = proxy.GetEntry(RequestID);
                    }
                    if (Operate == "Edit")
                    {
                        ViewData["Operate"] = "Edit";
                    }
                    else if (Operate == "View")
                    {
                        ViewData["Operate"] = "View";
                    }
                }

            }
            return View(WS_Incident_Journal);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult JournalDetail_SaveJournal(WS_Incident_Journal WS_Incident_Journal)
        {


            string Operate = Request.Form["hidOperate"];
            string RequestID = Request.Form["hidRequestID"];

            using (ARLoginContext context = new ARLoginContext(ARserver, ARuser, ARpwd))
            {
                ARProxy<WS_Incident_Journal> proxy = new ARProxy<WS_Incident_Journal>(context);
                if (Operate == "Add")
                {
                    proxy.CreateEntry(WS_Incident_Journal);
                }
                else
                {
                    WS_Incident_Journal.RequestID = RequestID;
                    WS_Incident_Journal.ChangedProperties.Clear();
                    WS_Incident_Journal.ChangedProperties.Add("Subject", true);
                    WS_Incident_Journal.ChangedProperties.Add("Body", true);
                    WS_Incident_Journal.ChangedProperties.Add("Category", true);
                    WS_Incident_Journal.ChangedProperties.Add("Source", true);
                    WS_Incident_Journal.ChangedProperties.Add("TimeSpent", true);
                    WS_Incident_Journal.ChangedProperties.Add("PublishToWeb", true);
                    WS_Incident_Journal.ChangedProperties.Add("CreateBy", true);
                    WS_Incident_Journal.ChangedProperties.Add("CreateTime", true);
                    WS_Incident_Journal.ChangedProperties.Add("LastModifyBy", true);
                    WS_Incident_Journal.ChangedProperties.Add("LastModifyDate", true);
                    WS_Incident_Journal.ChangedProperties.Add("ParentObejctID", true);
                    proxy.SetEntry(WS_Incident_Journal);
                }

            }
            Response.Write("<script>window.opener=null;window.close()</script>");

            //ContentResult content = new ContentResult();
            //content.Content = string.Format("<script language='javaScript' type='text/javaScript'>window.close()</script>");
            //return content;
            return new EmptyResult();
        }


        #region 规范方法
        [HttpGet]
        public ActionResult Form(string id)
        {
            //新增时ID是父单号，修改时ID是日志唯一ID
            var formModel = RouteData.Values[NRemedyUIExtension.FormModelKey];
            var type = RouteData.Values["type"];
            if (type != null && type.ToString() == "incidentjournal")
            {
                if (formModel != null && formModel.ToString() == "Detail")
                {
                    return IncidentJournalDetail(id);
                }
                else
                {
                    WS_Incident_Journal IncidentJournal = new WS_Incident_Journal();
                    IncidentJournal.ParentObejctID = id;
                    ViewData.Model = IncidentJournal;
                    return View("Incident_JournalCreate");
                }
            }
            else
            {
                return new EmptyResult();
            }

        }

        /// <summary>
        /// 事件日志编辑页
        /// </summary>
        /// <param name="RequestID"></param>
        /// <returns></returns>
        [NonAction]
        public ActionResult IncidentJournalDetail(string RequestID)
        {

            if (string.IsNullOrEmpty(RequestID))
            {
                throw new ArgumentNullException("RequestID");
            }

            var q = from s in new ARSet<WS_Incident_Journal>(ARUserSession.ARContext)
                    where s.RequestID == RequestID
                    select s;

            ViewData.Model = q.ToList().First();

            return View("Incident_JournalDetail");
        }

        /// <summary>
        /// 事件日志保存
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public ActionResult IncidentJournalSave(WS_Incident_Journal IncidentJournal, string formModel)
        {
            ARProxy<WS_Incident_Journal> proxy = new ARProxy<WS_Incident_Journal>(ARUserSession.ARContext);
            if (formModel == "Create")
            {
                string requestid = proxy.CreateEntry(IncidentJournal);

            }
            else
            {
                proxy.SetEntry(IncidentJournal, delegate(ARBaseForm model, PropertyInfo pi)
                {
                    //不更新ID
                    return !(pi.Name == "RequestID" || pi.Name == "ParentObejctID");
                });
            }
            Response.Write("<script>window.opener=null;window.close()</script>");

            return new EmptyResult();
        }
        #endregion
    }
}
